package cn.service;

import cn.constant.Messages;
import cn.entity.User;
import cn.repository.UserProjection;
import cn.repository.UserRepository;
import cn.util.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author m on 2023/1/27
 * Description:
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;


    @Autowired
    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }


    @Override
    public User check(String userName, String password) {
        return userRepository.findUserByUserNameAndPassword(userName,password);
    }

    @Override
    public boolean isUserNameOccupied(String userName) {
        return userRepository.countByUserName(userName)>0;
    }

    @Override
    public User register(User user) {
        return userRepository.save(user);
    }

    @Override
    public User getById(UUID id) {
        User user = userRepository.findById(id).orElse(null);
        return user;
    }

    @Override
    public String getSalt(String userName) {
        //通过用户名查找用户
        List<UserProjection> users = userRepository.findUsersByUserName(userName);

        if (users == null || users.size() == 0){
            return "";
        }

        UserProjection user = users.get(0);
        if (user == null){
            return "";
        }
        String salt = user.getSalt();
        //如果数据库中存在该用户，就返回该用户的salt
        return salt == null ? "" : salt;
    }

    @Override
    public Messages uploadAvatar(MultipartFile file,String token) throws IOException {
        User user = userRepository.findById(AuthUtils.getUser(token).getId()).orElse(null);
        if(user==null){
            return Messages.USER_NOT_EXISTS;
        }
        //此StringUtils为系统类    获得文件名
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());

        user.setAvatar(file.getBytes());

        return Messages.SUCCESS;
    }


    @Override
    public List<User> get() {
        return userRepository.findAllByOrderByUpdateTimeDesc();
    }

    @Override
    public List<User> get(int page, int size) {
        return userRepository.findAllByOrderByUpdateTimeDesc(PageRequest.of(page,size));
    }

    @Override
    public Integer count() {
        return userRepository.countAllBy();
    }

    @Override
    public List<User> search(String kw) {
        return userRepository.findByQuery(kw);
    }

    @Override
    public List<User> isApplying(boolean is) {
        return userRepository.findUsersByApplyOrderByUpdateTimeDesc(is);
    }


    @Override
    public User approveUser(UUID id) {
        User user = userRepository.findById(id).orElse(null);
        if(user != null){
            user.setApply(false);
            user.setType(User.TYPE_CERT);
            user.setCertTime(new Date());
            userRepository.save(user);
            return user;
        }
        return null;
    }

    @Override
    public User declinedUser(UUID id) {
        User user = userRepository.findById(id).orElse(null);
        if(user != null){
            user.setApply(false);
            userRepository.save(user);
            return user;
        }
        return null;
    }

    @Override
    public User prohibitUser(UUID id) {
        User user = userRepository.findById(id).orElse(null);
        if(user != null){
            user.setApply(false);
            user.setType(User.TYPE_BANNED);
            userRepository.save(user);
            return user;
        }
        return null;
    }
}
